package org.jxc.web.action;

import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.Customer;
import org.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.domain.Dictionary;
import org.team4.exception.CRUDException;
import org.team4.service.DictionaryService;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class CustomerAction extends GenericAction<Customer> {

	private static final long serialVersionUID = -4009682687327287166L;

	@Autowired
	private CustomerService customerService;
	@Autowired
	private DictionaryService dictionaryService;
	
	private List<Dictionary> typeList;
	private List<Dictionary> regionList;
	private List<Dictionary> billTypeList;
	private List<Dictionary> payTypeList;
	private Customer customer;
	
	public String listPagination() throws Exception {
		page = customerService.listPagionation(page);
		return SUCCESS;
	}
	
	public String choose() throws Exception {
		page = customerService.listPagionation(page);
		return SUCCESS;
	}
	
	public String toSave() throws Exception {
		typeList = dictionaryService.findDictionaryBySuperId(JXCString.C_TYPE);
		regionList = dictionaryService.findDictionaryBySuperId(JXCString.REGION);
		payTypeList = dictionaryService.findDictionaryBySuperId(JXCString.PAY_TYPE);
		billTypeList = dictionaryService.findDictionaryBySuperId(JXCString.BILL_TYPE);
		return SUCCESS;
	}
	
	public String toUpdate() throws Exception {
		typeList = dictionaryService.findDictionaryBySuperId(JXCString.C_TYPE);
		regionList = dictionaryService.findDictionaryBySuperId(JXCString.REGION);
		payTypeList = dictionaryService.findDictionaryBySuperId(JXCString.PAY_TYPE);
		billTypeList = dictionaryService.findDictionaryBySuperId(JXCString.BILL_TYPE);
		customer = customerService.findCustomerById(customer.getId());
		return SUCCESS;
	}
	
	public String save() throws Exception {
		customerService.saveCustomer(customer);
		return SUCCESS;
	}
	
	public String update() throws Exception {
		customerService.update(customer);
		return SUCCESS;
	}
	
	public String delete() throws CRUDException{
		customerService.delete(customer.getId());
		return SUCCESS;
	}

	public String view() throws CRUDException{
		customer = customerService.findCustomerById(customer.getId());
		return SUCCESS;
	}
	
	
	public List<Dictionary> getTypeList() {
		return typeList;
	}
	public List<Dictionary> getRegionList() {
		return regionList;
	}
	public List<Dictionary> getBillTypeList() {
		return billTypeList;
	}
	public List<Dictionary> getPayTypeList() {
		return payTypeList;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
}
